package article

import (
	ArticleModel "blog/app/model/articles"
	"blog/core"
	"strings"

	"github.com/go-playground/validator/v10"
)

// 添加文章
type Create struct {
	Title      string `validate:"required" validateErr:"标题不能为空"`
	SubTitle   string
	Cid        int    `validate:"required,catExists" validateErr:"分类不能为空,分类不存在"`
	Tid        string `validate:"required,tagExists" validateErr:"标签不能为空,有相关标签不存在"`
	MainImg    string `validate:"required_if=Type 0"`
	Pwd        int
	IsPublish  int
	Type       int
	IsComment  int
	IsOriginal int
	Content    string
	Desc       string
}

func (c *Create) Validate(validate *validator.Validate) {
	validate.RegisterValidation("catExists", func(fl validator.FieldLevel) bool {
		var count int64
		core.DbConnect.Model(&ArticleModel.Cate{}).Where("id = ?", fl.Field().Int()).Count(&count)
		return count > 0
	})

	validate.RegisterValidation("tagExists", func(fl validator.FieldLevel) bool {
		var tagId string = fl.Field().String()
		var count int64
		tagIds := strings.Split(tagId, ",")
		core.DbConnect.Model(&ArticleModel.Tag{}).Where("id in ?", tagIds).Count(&count)
		return int(count) >= len(tagIds)
	})
}

// 文章列表
type Lists struct {
	Page  int
	Limit int
	Title string
}
